<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Multipage.php 130 2009-05-21 14:23:24Z movoin $
 */

//{{include
FLEA::loadClass('Controller_Base');
//}}

class Controller_Multipage extends Controller_Base
{
    var $modelSinglepage;

    function Controller_Multipage()
    {
        parent::Controller_Base();
        $this->modelSinglepage = & FLEA::getSingleton( 'Model_Singlepage' );
    }

    function actionIndex()
    {
        $this->_setBack();
        $data['focus'] = 'column';
        $data['column'] = $this->getColumnInf( $_GET[FLEA::getAppInf( 'columnAccessor' )] );
        $where = array(
            array( 'col_key', $_GET[FLEA::getAppInf( 'columnAccessor' )] ),
            array( 'site_id', (int)$_GET['site'] )
            );
        $table =& $this->modelSinglepage->getTable();
        $data['rows'] = $table->findAll($where, 'sort_id ASC, id ASC', null, 'id,title,created');
        $this->_executeView('multipage', $data);
    }

    /**
     * 新增页面
     *
     */
    function actionAdd()
    {
        $this->_setBack();
        $table =& $this->modelSinglepage->getTable();
        $row = $this->_prepareData($table->meta);
        $this->_editPage($row);
    }

    /**
     * 修改页面
     *
     */
    function actionEdit()
    {
        $this->_setBack();
        $row = $this->modelSinglepage->getContent($_GET[FLEA::getAppInf( 'columnAccessor' )], $_GET['site'], $_GET['id']);
        $this->_editPage($row);
    }

    /**
     * 保存页面
     *
     */
    function actionSave()
    {
        if($_POST['title']) {
            js_alert($this->modelSinglepage->updateContent(&$_POST), 0, $this->_getBack());
        } else {
            js_alert('页面标题未填写!', 0, $this->_getBack());
        }
    }

    /**
     * 页面排序
     *
     */
    function actionSort()
    {
        $this->_setBack();
        $data['focus'] = 'column';
        $data['column'] = $this->getColumnInf( $_GET[FLEA::getAppInf( 'columnAccessor' )] );
        $this->_executeView('content-sort', $data);
    }

    /**
     * 保存排序结果
     *
     */
    function actionSaveSort()
    {
        js_alert($this->modelSinglepage->updateSort(&$_POST['seqNoList']), 0, $this->_getBack());
    }

    /**
     * 删除页面
     *
     */
    function actionRemove()
    {
        js_alert($this->modelSinglepage->remove(&$_GET['id']), 0, $this->_getBack());
    }

    function _editPage($row)
    {
        $data['focus'] = 'column';
        $data['column'] = $this->getColumnInf( $_GET[FLEA::getAppInf( 'columnAccessor' )] );
        $data['row'] = $row;
        $this->_executeView('multipage-edit', $data);
    }

}
